package com.ruoyi.web.controller.applet;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.OperatorType;
import com.ruoyi.system.domain.BdmBanner;
import com.ruoyi.system.domain.BdmGood;
import com.ruoyi.system.domain.dto.BdmGoodDto;
import com.ruoyi.system.domain.vo.applet.AppletBdmGoodVo;
import com.ruoyi.system.service.IBdmBannerService;
import com.ruoyi.system.service.IBdmGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 小程序首页Controller
 *
 * @author ruoyi
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/applet/home")
public class AppletHomeController extends BaseController
{
    @Autowired
    private IBdmBannerService bdmBannerService;

    @Autowired
    private IBdmGoodService bdmGoodService;

    /**
     * 小程序首页banner列表
     */
    @GetMapping("/appletBannerList")
    public AjaxResult appletList(BdmBanner bdmBanner)
    {
        bdmBanner.setShowOrNot(1L);
        List<BdmBanner> list = bdmBannerService.selectBdmBannerList(bdmBanner);
        return success(list);
    }

    /**
     * 首页推荐列表
     * @param
     * @return
     */
    @GetMapping("/referralsList")
    public AjaxResult referralsList(BdmGoodDto bdmGoodDto)
    {
        bdmGoodDto.setIsTop("1");
//        return success(bdmGoodService.selectHomeReferralsGoodList(bdmGood));
        return success(bdmGoodService.listGoodVo(bdmGoodDto));
    }

    /**
     * 商品推荐列表
     * @param bdmGoodDto
     * @return
     */
    @GetMapping("/referralsGoodList")
    public AjaxResult referralsGoodList(BdmGoodDto bdmGoodDto)
    {
//        return success(bdmGoodService.selectHomeReferralsGoodList(bdmGood));
        List<AppletBdmGoodVo> list = bdmGoodService.listGoodVo(bdmGoodDto);
        Collections.shuffle(list);
        return success(Optional.of(list)
                .orElse(Collections.emptyList())
                .stream().limit(6).collect(Collectors.toList()));
    }

    /**
     * 首页搜索
     * @param search
     * @return
     */
    @Log(title = "首页搜索", operatorType = OperatorType.MOBILE)
    @GetMapping("/searchItem")
    public AjaxResult searchItem(String search)
    {
        List<BdmGood> list = bdmGoodService.selectHomeContents(search);
        return success(list);
    }

}
